package kr.co.skplanet.commute.security.annotation;

import org.springframework.core.MethodParameter;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.support.WebArgumentResolver;
import org.springframework.web.context.request.NativeWebRequest;

/**
 * {SpringSecurity}
 * 
 * @author kickscar
 *
 */
public class AuthUserWebArgumentResolver implements WebArgumentResolver {
	@Override
	public Object resolveArgument(MethodParameter param, NativeWebRequest request) throws Exception {
		AuthUser authUser = param.getParameterAnnotation(AuthUser.class);
		if (authUser != null) {
			
			Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			
			if (principal.getClass() == String.class) {
				return null;
			} else {
				return principal;
			}
		}
		
		return WebArgumentResolver.UNRESOLVED;
	}
}
